package com.ungs.form;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.ungs.bo.DireccionBO;
import com.ungs.bo.PersonaBO;
import com.ungs.bo.impl.DireccionBOImpl;
import com.ungs.bo.impl.PersonaBOImpl;
import com.ungs.dao.ProveedorDAO;
import com.ungs.dao.impl.ProveedorDAOImpl;
import com.ungs.entities.Direccion;
import com.ungs.entities.Persona;
import com.ungs.entities.Proveedor;
import com.ungs.utils.Constantes;
import com.ungs.utils.ValidacionCampo;

public class ModificarVerProveedorForm extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtRazonSocial;
	private JTextField txtCuit;
	private JTextField txtTelefono;
	private JTextField txtPais;
	private JTextField txtProvincia;
	private JTextField txtLocalidad;
	private JTextField txtCalle;
	private JTextField txtNumero;
	private JTextField txtMail;
	private JTextField txtPCMail;
	private JTextField txtPCTel;
	private JTextField txtPCNombre;
	private JTextField txtPCApellido;
	private JComboBox jcbCondicionIva;
	private Proveedor proveedor;

	public ModificarVerProveedorForm(JDialog parent, Proveedor p) {
		super(parent, true);
		setResizable(false);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(parent);
		this.proveedor = p;
		crear();
		llenarCampos();
	}

	private void crear() {
		setTitle("Modificar Proveedor");
		setBounds(100, 100, 610, 541);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(10, 11, 583, 472);
		contentPanel.add(panel_1);
		panel_1.setLayout(null);

		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setToolTipText("");
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(

		0, 0, 0)), " Proveedor ", TitledBorder.LEADING,

		TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_2.setBounds(10, 11, 565, 289);
		panel_1.add(panel_2);

		txtRazonSocial = new JTextField();
		txtRazonSocial.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e,
						txtRazonSocial, 30);
			}
		});
		txtRazonSocial.setBounds(99, 21, 156, 20);
		panel_2.add(txtRazonSocial);

		JLabel label = new JLabel("Raz\u00F3n social:");
		label.setBounds(21, 24, 78, 14);
		panel_2.add(label);

		JLabel label_1 = new JLabel(" Cond. IVA:");
		label_1.setBounds(21, 92, 57, 14);
		panel_2.add(label_1);

		txtCuit = new JTextField();
		txtCuit.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.validarCuit(e, txtCuit);
				ValidacionCampo.verificarLargoCamposJTextField(e, txtCuit, 13);
			}
		});
		txtCuit.setBounds(373, 21, 135, 20);
		panel_2.add(txtCuit);

		JLabel label_2 = new JLabel("CUIT:");
		label_2.setBounds(313, 24, 38, 14);
		panel_2.add(label_2);

		txtTelefono = new JTextField();
		txtTelefono.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtTelefono,
						30);
			}
		});
		txtTelefono.setBounds(371, 59, 137, 20);
		panel_2.add(txtTelefono);

		JLabel label_3 = new JLabel("Tel\u00E9fono:");
		label_3.setBounds(313, 62, 46, 14);
		panel_2.add(label_3);

		JPanel panel_3 = new JPanel();
		panel_3.setLayout(null);
		panel_3.setBorder(new TitledBorder(new LineBorder(

		new Color(0, 0, 0)), " Direcci\u00F3n ",

		TitledBorder.LEADING, TitledBorder.TOP, null,

		new Color(0, 0, 0)));
		panel_3.setBounds(10, 120, 545, 158);
		panel_2.add(panel_3);

		JLabel label_4 = new JLabel("   Pa\u00EDs:");
		label_4.setVerticalAlignment(SwingConstants.BOTTOM);
		label_4.setBounds(257, 60, 48, 14);
		panel_3.add(label_4);

		txtPais = new JTextField();
		txtPais.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				eventoFocoGanadoPaisProveedor();
			}
		});
		txtPais.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtPais, 30);
			}
		});
		txtPais.setBounds(315, 57, 144, 20);
		panel_3.add(txtPais);

		JLabel label_5 = new JLabel("Provincia:");
		label_5.setBounds(257, 26, 55, 14);
		panel_3.add(label_5);

		txtProvincia = new JTextField();
		txtProvincia.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				eventoFocoGanadoProvinciaProveedor();
			}
		});
		txtProvincia.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtProvincia,
						30);
			}
		});
		txtProvincia.setBounds(322, 23, 144, 20);
		panel_3.add(txtProvincia);

		JLabel label_6 = new JLabel(" Localidad:");
		label_6.setBounds(10, 96, 55, 14);
		panel_3.add(label_6);

		txtLocalidad = new JTextField();
		txtLocalidad.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtLocalidad,
						30);
			}
		});
		txtLocalidad.setBounds(67, 90, 156, 20);
		panel_3.add(txtLocalidad);

		JLabel label_7 = new JLabel("Calle:");
		label_7.setBounds(10, 29, 36, 14);
		panel_3.add(label_7);

		txtCalle = new JTextField();
		txtCalle.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtCalle, 30);
			}
		});
		txtCalle.setBounds(68, 20, 155, 20);
		panel_3.add(txtCalle);

		JLabel label_8 = new JLabel(" N\u00FAmero:");
		label_8.setBounds(10, 60, 48, 14);
		panel_3.add(label_8);

		txtNumero = new JTextField();
		txtNumero.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarSoloNumeros(e);
				ValidacionCampo.verificarLargoCamposJTextField(e, txtNumero, 8);
			}
		});
		txtNumero.setBounds(69, 54, 55, 20);
		panel_3.add(txtNumero);

		JLabel label_18 = new JLabel(
				"Si el campo calle esta vacio no se guarda la direccion.");
		label_18.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_18.setBounds(10, 133, 401, 14);
		panel_3.add(label_18);

		JLabel label_9 = new JLabel("Mail:");
		label_9.setBounds(21, 55, 28, 14);
		panel_2.add(label_9);

		txtMail = new JTextField();
		txtMail.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtMail, 60);
			}
		});
		txtMail.setBounds(99, 52, 156, 20);
		panel_2.add(txtMail);

		jcbCondicionIva = new JComboBox();
		jcbCondicionIva.setModel(new DefaultComboBoxModel(new String[] {
				"EXENTO", "RESP. INSCRIPTO", "MONOTRIBUTO" }));
		jcbCondicionIva.setBounds(99, 88, 156, 22);
		panel_2.add(jcbCondicionIva);

		JLabel label_10 = new JLabel("*");
		label_10.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_10.setBounds(265, 24, 28, 14);
		panel_2.add(label_10);

		JLabel label_11 = new JLabel("*");
		label_11.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_11.setBounds(518, 24, 28, 14);
		panel_2.add(label_11);

		JPanel panel_4 = new JPanel();
		panel_4.setLayout(null);
		panel_4.setBorder(new TitledBorder(new LineBorder(

		new Color(0, 0, 0)), " Persona de Contacto ",

		TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0,

		0, 0)));
		panel_4.setBounds(10, 311, 565, 125);
		panel_1.add(panel_4);

		JLabel label_12 = new JLabel("Mail:");
		label_12.setBounds(270, 67, 28, 14);
		panel_4.add(label_12);

		txtPCMail = new JTextField();
		txtPCMail.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo
						.verificarLargoCamposJTextField(e, txtPCMail, 60);
			}
		});
		txtPCMail.setBounds(335, 61, 190, 20);
		panel_4.add(txtPCMail);

		JLabel label_13 = new JLabel("Tel\u00E9fono:");
		label_13.setBounds(10, 67, 46, 14);
		panel_4.add(label_13);

		txtPCTel = new JTextField();
		txtPCTel.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtPCTel, 30);
			}
		});
		txtPCTel.setBounds(75, 61, 152, 20);
		panel_4.add(txtPCTel);

		txtPCNombre = new JTextField();
		txtPCNombre.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e, txtPCNombre,
						30);
			}
		});
		txtPCNombre.setBounds(75, 30, 152, 20);
		panel_4.add(txtPCNombre);

		JLabel label_14 = new JLabel("Nombre:");
		label_14.setBounds(10, 33, 55, 14);
		panel_4.add(label_14);

		JLabel label_15 = new JLabel("Apellido:");
		label_15.setBounds(270, 33, 55, 14);
		panel_4.add(label_15);

		txtPCApellido = new JTextField();
		txtPCApellido.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				ValidacionCampo.verificarLargoCamposJTextField(e,
						txtPCApellido, 30);
			}
		});
		txtPCApellido.setBounds(335, 30, 190, 20);
		panel_4.add(txtPCApellido);

		JLabel label_17 = new JLabel(
				"Si el campo nombre esta vacion no se gurda la persona.");
		label_17.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_17.setBounds(10, 99, 401, 14);
		panel_4.add(label_17);

		JLabel label_16 = new JLabel("Los campos con * son obligatorios");
		label_16.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_16.setBounds(10, 447, 401, 14);
		panel_1.add(label_16);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Aceptar");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						eventoBotonAceptar();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancelar");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						eventoBotonCancelar();
					}
				});
				buttonPane.add(cancelButton);
			}
		}
	}

	private void llenarCampos() {

		txtRazonSocial.setText(proveedor.getRazonSocial());
		txtCuit.setText(proveedor.getCuit());
		txtMail.setText(proveedor.getMail());
		txtTelefono.setText(proveedor.getTelefono());
		jcbCondicionIva.setSelectedItem(proveedor.getCondicionIva());

		if (proveedor.getDireccion() != null) {
			txtPais.setText(proveedor.getDireccion().getPais());
			txtProvincia.setText(proveedor.getDireccion().getProvincia());
			txtLocalidad.setText(proveedor.getDireccion().getLocalidad());
			txtCalle.setText(proveedor.getDireccion().getCalle());
			txtNumero.setText(String.valueOf(proveedor.getDireccion()
					.getNumero()));
		}

		if (proveedor.getPersonaContacto() != null) {
			txtPCNombre.setText(proveedor.getPersonaContacto().getNombre());
			txtPCApellido.setText(proveedor.getPersonaContacto().getApellido());
			txtPCMail.setText(proveedor.getPersonaContacto().getMail());
			txtPCTel.setText(proveedor.getPersonaContacto().getTelefono());
		}
	}

	private void eventoBotonAceptar() {
		ProveedorDAO dao = new ProveedorDAOImpl();

		boolean cuitCorrecto = false;
		if (txtCuit.getText().length() == 13) {
			cuitCorrecto = true;
		}
		if (!txtRazonSocial.getText().trim().isEmpty() && cuitCorrecto) {
			if (proveedor.getDireccion() != null) {
				// Si tiene direccion
				if (!txtCalle.getText().trim().isEmpty()) {
					proveedor.getDireccion().setCalle(
							txtCalle.getText().toUpperCase());
					proveedor.getDireccion()
							.setNumero(
									Integer.parseInt(txtNumero.getText()
											.toUpperCase()));
					proveedor.getDireccion().setLocalidad(
							txtLocalidad.getText().toUpperCase());
					proveedor.getDireccion().setProvincia(
							txtProvincia.getText().toUpperCase());
					proveedor.getDireccion().setPais(
							txtPais.getText().toUpperCase());
				} else {
					// Si tiene direccion pero no hay nada en los campos
					DireccionBO direccionBO = new DireccionBOImpl();
					direccionBO.eliminarDireccion(proveedor.getDireccion());
					proveedor.setDireccion(null);
				}
			} else {
				// Si no tiene direccion
				if (!txtCalle.getText().trim().isEmpty()) {
					Direccion direccion = new Direccion();
					direccion.setCalle(txtCalle.getText().toUpperCase());
					direccion.setNumero(Integer.parseInt(txtNumero.getText()
							.toUpperCase()));
					direccion
							.setLocalidad(txtLocalidad.getText().toUpperCase());
					direccion
							.setProvincia(txtProvincia.getText().toUpperCase());
					direccion.setPais(txtPais.getText().toUpperCase());
					proveedor.setDireccion(direccion);
				}
			}

			if (proveedor.getPersonaContacto() != null) {
				if (!txtPCNombre.getText().trim().isEmpty()) {
					proveedor.getPersonaContacto().setNombre(
							txtPCNombre.getText().toUpperCase());
					proveedor.getPersonaContacto().setApellido(
							txtPCApellido.getText().toUpperCase());
					proveedor.getPersonaContacto().setTelefono(
							txtPCTel.getText().toUpperCase());
					proveedor.getPersonaContacto().setMail(
							txtPCMail.getText().toUpperCase());
				} else {
					PersonaBO personaBO = new PersonaBOImpl();
					personaBO.eliminarPersona(proveedor.getPersonaContacto());
					proveedor.setPersonaContacto(null);
				}
			} else {
				if (!txtPCNombre.getText().trim().isEmpty()) {
					Persona persona = new Persona();
					persona.setNombre(txtPCNombre.getText().toUpperCase());
					persona.setApellido(txtPCApellido.getText().toUpperCase());
					persona.setMail(txtPCMail.getText().toUpperCase());
					persona.setTelefono(txtPCTel.getText().toUpperCase());
					proveedor.setPersonaContacto(persona);
				}
			}

			proveedor.setCuit(txtCuit.getText());
			proveedor.setRazonSocial(txtRazonSocial.getText());
			proveedor.setMail(txtMail.getText());
			proveedor.setTelefono(txtTelefono.getText());

			dao.actualizar(proveedor);
			this.dispose();
		} else {
			JOptionPane.showMessageDialog(this,
					"Complete los campos obligatorios:\nRazonSocial\nCUIT",
					"Campos Obligatorios", JOptionPane.WARNING_MESSAGE);
		}
	}

	private void eventoBotonCancelar() {
		this.dispose();
	}

	private void eventoFocoGanadoProvinciaProveedor() {
		if (txtProvincia.getText().trim().isEmpty()
				&& !txtCalle.getText().trim().isEmpty()) {
			txtProvincia.setText(Constantes.PROVINCIA);
		}
		txtProvincia.selectAll();
	}

	private void eventoFocoGanadoPaisProveedor() {
		if (txtPais.getText().trim().isEmpty()
				&& !txtCalle.getText().trim().isEmpty()) {
			txtPais.setText(Constantes.PAIS);
		}
		txtPais.selectAll();
	}
}
